const messageModel = require('../model/messageModel.js')

async function adminSendGlobal(ctx){
    let adminID = ctx.user.id
    let time = Date.now()
    let message = ctx.request.body.message

    if(!message){
        return {code:0,msg:'请输入内容',data:''}
    }

    let res = await messageModel.adminSendGlobal({adminID,time,message})
    ctx.body = res
}

async function adminSendSingle(ctx){
    let adminID = ctx.user.id
    let uid = ctx.request.body.uid
    let message = ctx.request.body.message
    
    if(!message){
        return ctx.body = {code:0,msg:'请输入内容',data:''}
    }

    if(!uid){
        return ctx.body = {code:0,msg:'请选择目标用户',data:''}
    }

    let res = await messageModel.adminSendSingle({adminID,uid,message})
    ctx.body = res
}

async function usermessageList(ctx){
    let id = ctx.user.id
    let res = await messageModel.usermessageList(id)
    ctx.body = res
}   

async function adminList(ctx){
    let id = parseInt(ctx.query.id)
    let page = parseInt(ctx.query.page) || 1
    let num = parseInt(ctx.query.num) || 1000
    let offset = (page-1)*num
    if(!id || id == 1 || id == 2 || id == 3 ){
        let res = await messageModel.adminList({num,offset,id})
        ctx.body = res
    }
    
}

async function admindel(ctx){
    //站内信id
    let id = ctx.query.id
    if(!id){
        return ctx.body = {code:0,msg:'请选择要删除的站内信',data:''}
    }

    let res = await messageModel.admindel(id)
    ctx.body = res
}

async function read(ctx){
    //站内信id
    let id = ctx.request.body.id
    let uid = ctx.user.id
    if(!id){
        return ctx.body = {code:0,msg:'请选择站内信id'}
    }

    let res = await messageModel.read({id,uid})
    ctx.body = res
}

async function userdel(ctx){
    let id = ctx.query.id
    let uid = ctx.user.id

    let res = await messageModel.userdel({id,uid})
    ctx.body = res
}

async function reads(ctx){
    let uid = ctx.user.id
    let res = await messageModel.reads(uid)
    ctx.body = res
}
module.exports = {adminSendGlobal,adminSendSingle,usermessageList,adminList,admindel,read,userdel,reads}